from task import Task
from simulator_ddl import simulation_ddl
from simulator_ins_act import simulation_ins
from simulator_srpt import simulation_srpt
from simulator_feedback import simulation_wp
from simulator_rm import simulation_rm
from simulator_srpt_all import simulation_srpt_all
from simulator_srpt_edf import simulation_srpt_edf
from simulator_fifo import simulation_fifo
from simulator_fifo_edf import simulation_fifo_edf
from data_process import cr



import os
import sys
import csv
import math
import threading

path = f'FeasibleTest2/4_9_48/'

def get_fuck_info(log_filename, key_name):
    tail = os.popen(f'tail -n 5 {log_filename}')
    tail_list = tail.read().splitlines()
    for res in tail_list:
        key, val = res.split(':')
        if key == key_name:
            return eval(val)

def get_t_max(log_filename):
    tail = os.popen(f'head -n 5 {log_filename}')
    tail_list = tail.read().splitlines()
    for res in tail_list:
        key, val = res.split(':')
        if key == 't_max':
            return eval(val)

def get_final_result(log_filename):
    tail = os.popen(f'tail -n 5 {log_filename}')
    tail_list = tail.read().splitlines()
    for res in tail_list:
        if 'Final result' in res:
            key, val = res.split(':')
            if key == 'Final result':
                return eval(val)

def GetTaskData(exp):
    task_set = []
    print(exp,'-------------------')
    CompuCostlist = get_fuck_info(path + f'/test{exp}/tesk_set.log', 'computing_cost')
    print("CompuCostlist",CompuCostlist)
    UpCostlist = get_fuck_info(path + f'/test{exp}/tesk_set.log', 'upload_cost')[0]
    print("UpCostlist",UpCostlist)
    Mid_ddl_list = get_final_result(path + f'/test{exp}/ann_mid.log', )
    print("Mid_ddl",Mid_ddl_list)
    DDL_list = get_fuck_info(path + f'/test{exp}/tesk_set.log', 'Period')[0]
    print("DDL_list",DDL_list)
    Period_list = get_fuck_info(path + f'/test{exp}/tesk_set.log', 'Deadline')[0]
    print("Period_list",Period_list)
    for i in range(len(CompuCostlist)):
        c_i = CompuCostlist[i]
        r_i = UpCostlist[i]
        m_i = Mid_ddl_list[i]
        d_i = DDL_list[i]
        p_i = Period_list[i]
        task = Task(c_i,r_i,m_i,d_i,p_i)
        task_set.append(task)
    return task_set


def simu_all(exp):
    task_set = GetTaskData(exp)
    until = get_t_max(path + f'/test{exp}/ann_mid.log')
    print("until is:",until)
    file_name = f'/Users/liuchangkundeimac/real-time-scheduling/Schedulability2/4_9_48/test{exp}/'
    if not os.path.isfile(file_name):
        os.makedirs(file_name)
    edf_result = []
    ins_result = []
    srpt_result = []
    srpt_all_result = []
    srpt_edf_result = []
    feedback_result = []
    rm_result = []
    fifo_result = []
    fifo_edf_result = []
    num_job = []


    for i in range(1):
        result2,num_job2 = simulation_ddl(task_set,until,i,file_name)
        edf_result.append(result2)
        
        if result2 != -1:
            result3 = result2
        else:
            result3,num_job3 = simulation_ins(task_set,until,i,file_name)
        ins_result.append(result3)

        result4,num_job4,US4,CompRatio4,ACT4 = simulation_srpt(task_set,until,i,file_name)
        srpt_result.append(result4)

        result5,num_job5,US5,CompRatio5,ACT5 = simulation_wp(task_set,until,i,file_name)
        feedback_result.append(result5)

        result8,num_job8,US8,CompRatio8,ACT8 = simulation_rm(task_set,until,i,file_name)
        rm_result.append(result8)


        result9,num_job9,US9,CompRatio9,ACT9 = simulation_srpt_edf(task_set,until,i,file_name)
        srpt_edf_result.append(result9)
  

        result10,num_job10,US10,CompRatio10,ACT10  = simulation_srpt_all(task_set,until,i,file_name)
        srpt_all_result.append(result10)


        result11,num_job11,US11,CompRatio11,ACT11 = simulation_fifo(task_set,until,i,file_name)
        fifo_result.append(result11)

        result12,num_job12,US12,CompRatio12,ACT12 = simulation_fifo_edf(task_set,until,i,file_name)
        fifo_edf_result.append(result12)

        num_job.append(num_job2)
    cr(num_job,edf_result,ins_result,srpt_result,feedback_result,rm_result,srpt_edf_result,srpt_all_result,fifo_result,fifo_edf_result,file_name)
    
if __name__ == '__main__':
    for i in range(40):
        # t = threading.Thread(target=simu_all,args=(i,))
        # t.start()
        simu_all(i)
    